﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class CategoryDAO
    {
        DataServices m_CategoryDAO = new DataServices();

        public DataTable GetAll()
        {
            m_CategoryDAO.Load("GetAllCategory", null);
            return m_CategoryDAO;
        }

        public DataTable GetById(String cateId)
        {
            SqlParameter param = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param.Value = cateId;

            m_CategoryDAO.Load("GetCategoryById", param);
            return m_CategoryDAO;
        }

        public DataTable GetByLikeId(String cateId)
        {
            SqlParameter param = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param.Value = cateId;

            m_CategoryDAO.Load("GetCategoryByLikeId", param);
            return m_CategoryDAO;
        }

        public DataTable GetByName(String cateName)
        {
            SqlParameter param = new SqlParameter("@CateName", SqlDbType.NVarChar, 100);
            param.Value = cateName;

            m_CategoryDAO.Load("GetCategoryByName", param);
            return m_CategoryDAO;
        }

        public int Add(CategoryInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param[0].Value = info.CateId;

            param[1] = new SqlParameter("@CateName", SqlDbType.NVarChar, 100);
            param[1].Value = info.CateName;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return m_CategoryDAO.ExecuteUpdate("AddCategory", param);
        }

        public int Update(CategoryInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param[0].Value = info.CateId;

            param[1] = new SqlParameter("@CateName", SqlDbType.NVarChar, 100);
            param[1].Value = info.CateName;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return m_CategoryDAO.ExecuteUpdate("UpdateCategory", param);
        }

        public int Delete(String cateId)
        {
            SqlParameter param = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param.Value = cateId;

            return m_CategoryDAO.ExecuteUpdate("DeleteCategory", param);
        }

        public DataRow AddNewRow()
        {
            return m_CategoryDAO.NewRow();
        }

        public void AddNewCate(DataRow m_Row)
        {
            m_CategoryDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_CategoryDAO.ExecuteNoneQuery() > 0;
        }
    }
}
